<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { NInput, NButton, NRadioGroup, NRadioButton, NP } from 'naive-ui'

interface TodoItem {
  id: number
  text: string
  completed: boolean
}

const newTodo = ref<string>('')
const todoList = ref<TodoItem[]>([
  { id: 1, text: '学习Vue', completed: false },
  { id: 2, text: '完成练习', completed: false }
])
const filterType = ref<'all' | 'active' | 'completed'>('all')

// 监听待办列表变化
watch(todoList, (newVal) => {
  console.log('待办列表已更新', newVal)
  window.alert('待办列表已更新')
}, { deep: true })

// 新增待办事项
const addTodo = ()=> {
  if (newTodo.value.trim()) {
    todoList.value.push({
      id: Date.now(),
      text: newTodo.value.trim(),
      completed: false
    })
    newTodo.value = ''
  }
}

// 删除待办事项
const deleteTodo = (id: number)=> {
  todoList.value = todoList.value.filter(item => item.id !== id)
}

// 切换任务完成状态
const toggleComplete = (id: number) => {
  const todo = todoList.value.find(item => item.id === id)
  if (todo) {
    todo.completed = !todo.completed
  }
}

// 计算已完成数量
const completedCount = computed<number>(() => {
  return todoList.value.filter(item => item.completed).length
})

// 计算未完成数量
const activeCount = computed<number>(() => {
  return todoList.value.filter(item => !item.completed).length
})

// 筛选后的待办列表
const filteredTodos = computed<TodoItem[]>(() => {
  switch (filterType.value) {
    case 'active':
      return todoList.value.filter(item => !item.completed)
    case 'completed':
      return todoList.value.filter(item => item.completed)
    default:
      return todoList.value
  }
})
</script>

<template>
  <div class="container">
    <h1>待办事项</h1>

    <div class="stats">
      <span>已完成: {{ completedCount }}</span>
      <span>未完成: {{ activeCount }}</span>
    </div>

    <div class="input-group">
      <n-input v-model:value="newTodo"
               @keyup.enter="addTodo"
               placeholder="输入待办事项"
               class="todo-input"/>
      <n-button @click="addTodo" class="add-btn" type="primary">添加</n-button>
    </div>

    <!-- 筛选控件 -->
    <n-radio-group v-model:value="filterType" class="filter-group">
      <n-radio-button value="all">全部</n-radio-button>
      <n-radio-button value="active">未完成</n-radio-button>
      <n-radio-button value="completed">已完成</n-radio-button>
    </n-radio-group>

    <div v-if="filteredTodos.length" class="todo-list">
      <div v-for="item in filteredTodos" :key="item.id" class="todo-item">
        <span
            @click="toggleComplete(item.id)"
            :style="{ textDecoration: item.completed ? 'line-through' : 'none' }"
        >
          {{ item.text }}
        </span>
        <n-button @click="deleteTodo(item.id)" class="delete-btn" type="error">
          删除
        </n-button>
      </div>
    </div>
    <n-p v-else class="empty-tip">暂无待办事项</n-p>
  </div>
</template>

<style scoped>
.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
}

.stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 15px;
}

.input-group {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.filter-group {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.todo-input {
  flex: 1;
}

.todo-list {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
}

.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
  background: white;
}

.todo-item:last-child {
  border-bottom: none;
}

.todo-item span {
  flex: 1;
  cursor: pointer;
}

.empty-tip {
  text-align: center;
  color: #888;
  font-style: italic;
  margin-top: 20px;
}
</style>